﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;

namespace One_day.Controllers
{
    public class YichenController : Controller
    {
        // GET: Yichen
        /// <summary>
        /// 显示商城的所有物品种类
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            One_day.Models.YichenEntities shop = new Models.YichenEntities();
            List<One_day.Models.T_Shop_Category> list = shop.T_Shop_Category.ToList();
            ViewBag.lst = list;
            return View();
        }
        /// <summary>
        /// 一个种类的物品清单
        /// </summary>
        /// <param name="CatagoryId"></param>
        /// <returns></returns>
        public ActionResult List(int CatagoryId=1)
        {
            One_day.Models.YichenEntities shop = new Models.YichenEntities();
            //   =>lambda表达式，匿名函数，用于委托方面
            One_day.Models.T_Shop_Category db = shop.T_Shop_Category.Single(m => m.Id == CatagoryId);
            List<One_day.Models.Table> list = db.Table.ToList();
            ViewBag.lst = list;
            return View();
        }
        /// <summary>
        /// 商品具体详情
        /// </summary>
        /// <param name="CatagoryId"></param>
        /// <returns></returns>
        public ActionResult Content(int Id=1)
        {
            One_day.Models.YichenEntities yichen = new Models.YichenEntities();
            One_day.Models.Table t_Shop_ = yichen.Table.Single(m => m.Id == Id);
            ViewBag.lst = t_Shop_;
            return View();
        }
        
        public ActionResult ShopStore(int ProductId,int num)
        {
            One_day.Models.YichenEntities db = new Models.YichenEntities();
            if (Session["ticket"] != null)//登陆跳转到购物篮界面，反之跳转到注册登陆界面
            {
                One_day.Models.T_Base_User user = (One_day.Models.T_Base_User)Session["ticket"];
                if (ProductId != -1)//-1用于直接显示购物车中的内容
                {
                    //写入数据库数据
                    One_day.Models.Table product = db.Table.Single(m => m.Id == ProductId);//确定所要买的商品的具体内容
                    One_day.Models.T_Shop_Cart order = new Models.T_Shop_Cart();
                    order.ProductId = product.Id;
                    order.UserId = user.Id;
                    order.Amount = num;
                    order.Total_price = (int)product.Price*num;
                    order.ProductName = product.ProductName;
                    order.UserName = user.LoginName;
                    order.Time = DateTime.Now;//将订单提交的时间给后台数据库
                    db.T_Shop_Cart.Add(order);
                    db.SaveChanges();
                }
                //将更新过后的表T_Shop_Order显示出来
                List<One_day.Models.T_Shop_Cart> lst = db.T_Shop_Cart.Where(m => m.UserId == user.Id).ToList();
                var total = 0;
                foreach(var item in lst)
                {
                    total += (int)item.Total_price;
                }
                ViewBag.total = total;//购物车内所有物品所需花的钱
                ViewBag.lst = lst;
                
                return View();
            }
                
            else
                return RedirectToAction("Login", "User");
        }
        
       
        public JsonResult Settle_account()
        {
            One_day.Models.YichenEntities db = new Models.YichenEntities();
            //判断用户数据是否填写完整，没填写必要数据转到填写必要数据
            One_day.Models.T_Base_User user = (One_day.Models.T_Base_User)Session["ticket"];
            if (user.PhoneNumber == null)//电话号码为空转到个人中心填写数据
            {
                return Json(new { code = 3, message = "个人信息不全" });
            }
            if (user.Address == null)//收货地址为空跳转到收货地址编辑界面
            {
                return Json(new { code = 2, message = "收货地址缺失" });
            }
            try
            {
                List<One_day.Models.T_Shop_Cart> cart = db.T_Shop_Cart.Where(m => m.UserId == user.Id).ToList();
                foreach (var item in cart)
                {
                    One_day.Models.T_Shop_Order order = new Models.T_Shop_Order();
                    order.ProductId = item.ProductId;
                    order.ProductName = item.ProductName;
                    order.UserId = item.UserId;
                    order.UserName = item.UserName;
                    order.Amount = item.Amount;
                    order.Total_price = (int)item.Total_price;
                    order.Time = DateTime.Now;
                    db.T_Shop_Order.Add(order);
                    db.SaveChanges();
                }
                return Json(new { code = 1, message = "下单成功" });
            }
            catch(Exception e)
            {
               return  Json(new { code = 0, message = e.Message });
            }
        }
        public  ActionResult Settle_Success()
        {
            One_day.Models.YichenEntities db = new Models.YichenEntities();
            One_day.Models.T_Base_User user = (One_day.Models.T_Base_User)Session["ticket"];
            List<One_day.Models.T_Shop_Cart> cart = db.T_Shop_Cart.Where(m => m.UserId == user.Id).ToList();
            foreach(var item in cart)
            {
                One_day.Models.Table table = db.Table.Single(m => m.Id == item.ProductId);
                table.SelledAmount += item.Amount;
                table.StockAmount -= item.Amount;
                db.T_Shop_Cart.Remove(item);
            }
            db.SaveChanges();
            return View();
        }
    }
}